package root.friendship;

import root.user.User;

/**
 * Representa uma amizade entre dois usuarios.
 */
public class Friendship {
	
	private FriendshipState friendshipState;
	private User requester, target;
	
	public Friendship(User requester, User target) {
		this(requester, target, FriendshipState.Requested);
	}
	
	public Friendship(User requester, User target, FriendshipState state) {
		this.requester = requester;
		this.target = target;
		this.friendshipState = state;		
	}
	
	/**
	 * Retorna o usuario que pediu a relacao
	 * @return objeto User
	 */
	public User getRequester() {
		return requester;
	}
	
	/**
	 * Retorna o usuario alvo da relacao
	 * @return objeto de User
	 */
	public User getTarget() {
		return target;
	}
	
	/**
	 * Retorna o estado da relação
	 * @return objeto de FriendshipState
	 */
	public FriendshipState getFriendshipState() {
		return friendshipState;
	}
	
	/**
	 * Atribui um estado a relação
	 * @param state Novo estado
	 */
	public void setFriendshipState(FriendshipState state) {
		this.friendshipState = state;
	}
	
	public boolean isFriendship(User user, User user2){
		return (getRequester().equals(user) && getTarget().equals(user2) || getRequester().equals(user2) && getTarget().equals(user)); 
	}

	/**
	 * Duas amizades sao iguais quando os
	 * relacionados sao iguais.
	 * @param obj Objeto a ser comparado
	 * */
	public boolean equals(Object obj) {
		if (!(obj instanceof Friendship)) {
			return false;
		}
		Friendship comp = (Friendship)obj;
		return (comp.getRequester().equals(requester) && comp.getTarget().equals(target) || comp.getRequester().equals(target) && comp.getTarget().equals(requester)) &&comp.friendshipState.equals(friendshipState);
	}
	
	public int hashCode(){
		return friendshipState.hashCode() + requester.hashCode() + target.hashCode();
	}
}
